package com.audi.box.controller;

import java.util.ArrayList;

import android.content.Context;
import com.audi.box.adapter.SQLiteAsusHelperSmartTrigger;
import com.audi.box.service.SmartTriggerService;
import com.audi.box.service.SmartTriggerServiceImp;
import com.audi.box.service.SmartTriggerTriggerService;
import com.audi.box.service.SmartTriggerTriggerServiceImp;
import com.audi.box.service.SmartTriggerWhenService;
import com.audi.box.service.SmartTriggerWhenServiceImp;
import com.audi.box.service.SmartTriggerWhenTriggerService;
import com.audi.box.service.SmartTriggerWhenTriggerServiceImp;
import com.audi.dto.SmartTriggerItem;
import com.audi.dto.SmartTriggerTriggerItem;
import com.audi.dto.SmartTriggerWhenItem;
import com.audi.dto.SmartTriggerWhenTriggerItem;

/**
 * @author CuongPH7
 * 
 */
public class SmartTriggerController implements IController {
    private SmartTriggerService smartTrigger;
    private SmartTriggerWhenService stWhen;
    private SmartTriggerTriggerService stTrigger;
    private SmartTriggerWhenTriggerService stWhenTrigger;
    
    /**
     * Class constructor specifying Context to create
     * 
     * @param context
     *            android.content.Context
     * @throws Exception
     *             if can not create Smart Trigger Services
     */
    public SmartTriggerController(Context context) throws Exception {
        initService(new SQLiteAsusHelperSmartTrigger(context));
    }
    
    /**
     * Class constructor specifying Smart Trigger Services to create
     * 
     * @param whenService
     *            SmartTriggerWhenService
     * @param triggerService
     *            SmartTriggerTriggerService
     * @param whenTriggerTrigger
     *            SmartTriggerWhenTriggerService
     * @param smartTrigger
     *            SmartTriggerService
     * @throws Exception
     *             if (can not create Smart Trigger Services
     */
    public SmartTriggerController(SmartTriggerWhenService whenService,
            SmartTriggerTriggerService triggerService,
            SmartTriggerWhenTriggerService whenTriggerTrigger,
            SmartTriggerService smartTrigger) throws Exception {
        this.stWhen = whenService;
        this.stTrigger = triggerService;
        this.stWhenTrigger = whenTriggerTrigger;
        this.smartTrigger = smartTrigger;
    }
    
    private void initService(SQLiteAsusHelperSmartTrigger mSQLHelper)
            throws Exception {
        stWhen = new SmartTriggerWhenServiceImp(mSQLHelper);
        stTrigger = new SmartTriggerTriggerServiceImp(mSQLHelper);
        stWhenTrigger = new SmartTriggerWhenTriggerServiceImp(mSQLHelper);
        smartTrigger = new SmartTriggerServiceImp(mSQLHelper);
    }
    
    /**
     * Add Smart Trigger When Item
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return 1 if successful, 0 if SmartTrigger is not exist
     * @see #addSTTrigger(SmartTriggerTriggerItem)
     * @see #addSTWhenTrigger(SmartTriggerWhenTriggerItem)
     */
    public int addSTWhen(SmartTriggerWhenItem trigger) {
        return stWhen.addSmartTriggerWhen(trigger);
    }
    
    /**
     * Add Smart Trigger Trigger Item
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return {@code 1} if successful, {@code 0} if SmartTrigger is exist
     * @see #addSTWhen(SmartTriggerWhenItem)
     * @see #addSTWhenTrigger(SmartTriggerWhenTriggerItem)
     */
    public int addSTTrigger(SmartTriggerTriggerItem trigger) {
        return stTrigger.addSmartTriggerTrigger(trigger);
    }
    
    /**
     * Add Smart Trigger When-Trigger Item
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return 1 if successful, 0 if SmartTrigger is exist
     * @see #addSTTrigger(SmartTriggerTriggerItem)
     * @see #addSTWhen(SmartTriggerWhenItem)
     */
    public int addSTWhenTrigger(SmartTriggerWhenTriggerItem trigger) {
        return stWhenTrigger.addSmartTriggerWhenTrigger(trigger);
    }
    
    /**
     * Update Smart Trigger When Item
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return 1 if successful, 0 if SmartTrigger is exist
     * @see #updateSTTrigger(SmartTriggerTriggerItem)
     * @see #updateSTWhenTrigger(SmartTriggerWhenTriggerItem)
     */
    public int updateSTWhen(SmartTriggerWhenItem trigger) {
        return stWhen.updateSmartTriggerWhen(trigger);
    }
    
    /**
     * Update Smart Trigger Trigger Item
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return 1 if successful, 0 if SmartTrigger is not exist
     * @see #updateSTWhen(SmartTriggerWhenItem)
     * @see #updateSTWhenTrigger(SmartTriggerWhenTriggerItem)
     */
    public int updateSTTrigger(SmartTriggerTriggerItem trigger) {
        return stTrigger.updateSmartTriggerTrigger(trigger);
    }
    
    /**
     * Update Smart Trigger When-Trigger Item
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return 1 if successful, 0 if SmartTrigger is not exist
     * @see #updateSTWhen(SmartTriggerWhenItem)
     * @see #updateSTTrigger(SmartTriggerTriggerItem)
     */
    public int updateSTWhenTrigger(SmartTriggerWhenTriggerItem trigger) {
        return stWhenTrigger.updateSmartTriggerWhenTrigger(trigger);
    }
    
    /**
     * Delete Smart Trigger When Item
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return 1 if successful, 0 if SmartTrigger is not exist
     * @see #deleteSTTrigger(SmartTriggerTriggerItem)
     * @see #deleteSTWhenTrigger(SmartTriggerWhenTriggerItem)
     */
    public int deleteSTWhen(SmartTriggerWhenItem trigger) {
        return stWhen.deleteSmartTriggerWhen(trigger);
    }
    
    /**
     * Delete Smart Trigger Trigger Item
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return 1 if successful, 0 if SmartTrigger is not exist
     * @see #deleteSTWhen(SmartTriggerWhenItem)
     * @see #deleteSTWhenTrigger(SmartTriggerWhenTriggerItem)
     */
    public int deleteSTTrigger(SmartTriggerTriggerItem trigger) {
        return stTrigger.deleteSmartTriggerTrigger(trigger);
    }
    
    /**
     * Delete Smart Trigger When-Trigger Item
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return 1 if successful, 0 if SmartTrigger is not exist
     * @see #deleteSTWhen(SmartTriggerWhenItem)
     * @see #deleteSTTrigger(SmartTriggerTriggerItem)
     */
    public int deleteSTWhenTrigger(SmartTriggerWhenTriggerItem trigger) {
        return stWhenTrigger.deleteSmartTriggerWhenTrigger(trigger);
    }
    
    /**
     * Get Smart Trigger When Item
     * 
     * @param whenId
     *            SmartTriggerWhenItem Id
     * @return SmartTriggerWhenItem
     * @see #getSTTrigger(int)
     * @see #getSTWhenTrigger(int)
     */
    public SmartTriggerWhenItem getSTWhen(int whenId) {
        return stWhen.getSmartTrigger(whenId);
    }
    
    /**
     * Get Smart Trigger Trigger Item
     * 
     * @param triggerId
     *            SmartTriggerTriggerItem Id
     * @return SmartTriggerTriggerItem
     * @see #getSTWhen(int)
     * @see #getSTWhenTrigger(int)
     */
    public SmartTriggerTriggerItem getSTTrigger(int triggerId) {
        return stTrigger.getSmartTrigger(triggerId);
    }
    
    /**
     * Get Smart Trigger When-Trigger Item
     * 
     * @param whenTriggerId
     *            SmartTriggerWhenTriggerItem Id
     * @return SmartTriggerTriggerItem
     * @see #getSTWhen(int)
     * @see #getSTTrigger(int)
     */
    public SmartTriggerWhenTriggerItem getSTWhenTrigger(int whenTriggerId) {
        return stWhenTrigger.getSmartTrigger(whenTriggerId);
    }
    
    /**
     * Get Full Smart Trigger List
     * 
     * @return ArrayList SmartTriggerItem
     * @see #getSTWhenList()
     * @see #getSTTriggerList()
     * @see #getSTWhenTriggerList()
     * @see SmartTriggerItem
     */
    public ArrayList<SmartTriggerItem> getSmartTriggerFullList() {
        return smartTrigger.getListSmartTrigger();
    }
    
    /**
     * Get Smart Trigger When List
     * 
     * @return ArrayList When item
     * @see #getSTTriggerList()
     * @see #getSTWhenTriggerList()
     * @see #getSmartTriggerFullList()
     */
    public ArrayList<SmartTriggerWhenItem> getSTWhenList() {
        return stWhen.getListSmartTriggerWhen();
    }
    
    /**
     * Get Smart Trigger Trigger List
     * 
     * @return ArrayList When item
     * @see #getSTWhenList()
     * @see #getSTWhenTriggerList()
     * @see #getSmartTriggerFullList()
     */
    public ArrayList<SmartTriggerTriggerItem> getSTTriggerList() {
        return stTrigger.getListSmartTriggerTrigger();
    }
    
    /**
     * Get Smart Trigger When-Trigger List
     * 
     * @return ArrayList When item
     * @see #getSTWhenList()
     * @see #getSTTriggerList()
     * @see #getSmartTriggerFullList()
     */
    public ArrayList<SmartTriggerWhenTriggerItem> getSTWhenTriggerList() {
        return stWhenTrigger.getListSmartTriggerWhenTrigger();
    }
    
    /**
     * Set ON/OFF Smart Trigger List
     * 
     * @param trigger
     *            SmartTriggerWhenTriggerItem
     * @return 1 if successful, 0 if SmartTrigger is not exist
     */
    public int setOnOffSmartTrigger(SmartTriggerWhenTriggerItem trigger) {
        return stWhenTrigger.setOnOffSmartTrigger(trigger);
    }
    
    /**
     * Get Device value of Smart Trigger When
     * 
     * @param trigger
     *            SmartTriggerWhenItem
     * @return Device value, -1 if (Smart Trigger does not exist
     * @see #getDeviceValueFromSTTrigger(SmartTriggerTriggerItem)
     */
    public int getDeviceValueFromSTWhen(SmartTriggerWhenItem trigger) {
        return stWhen.getDeviceValueFromWhen(trigger);
    }
    
    /**
     * Get Device value of Smart Trigger When
     * 
     * @param trigger
     *            SmartTriggerTriggerItem
     * @return Device value, -1 if (Smart Trigger does not exist
     * @see #getDeviceValueFromSTWhen(SmartTriggerWhenItem)
     */
    public int getDeviceValueFromSTTrigger(SmartTriggerTriggerItem trigger) {
        return stTrigger.getDeviceValueFromTrigger(trigger);
    }
    
    /**
     * Get Smart Trigger Item
     * <p>
     * Add 08/09
     * 
     * @return SmartTriggerItem
     * @see #getSTWhen(int)
     * @see #getSTTrigger(int)
     */
    public SmartTriggerItem getSmartTrigger(int whenId, int triggerId) {
        return smartTrigger.getSmartTrigger(whenId, triggerId);
    }
    
    /**
     * Get Smart Trigger Item List
     * <p>
     * Add 08/09
     * 
     * @return SmartTriggerItem
     * @see #getSTWhen(int)
     * @see #getSTTrigger(int)
     */
    public ArrayList<SmartTriggerItem> getSmartTriggerList() {
        return smartTrigger.getListSmartTrigger();
    }
}
